package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.CarDao;
import com.hdaccp.xingmu.entity.Car;

import com.hdaccp.xingmu.entity.House;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car")
public class CarCon {

    @Autowired
    private CarDao carDao;

    @PersistenceContext
    private EntityManager entityManager ;

    @RequestMapping("/Qtiaozhuan")
    public String tiaozhuan(){
        return "WEB-INF/jsp/shequfuwu/cheweiguanli";
    }

    @RequestMapping("/Qttt")
    @ResponseBody
    public Map<String ,Object> quanbujinayi(int pageSize, int pageNumber,String type,String carid) {



        String jpal="FROM Car WHERE 1=1";
        if (  (!type.equals("-1")&& type!=null && type!="-1") ){
            jpal=jpal+" and type="+"'" + type +"'";
        }else if (carid!=null && !carid.equals("")){
            jpal=jpal+" and carid= "+"'" +carid+"'";
        }
        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(m) from Car m where 1=1 ";
        if (  (!type.equals("-1")&& type!=null && type!="-1") ){
            jpa=jpa+" and type="+"'" + type +"'";
        }else if (carid!=null && !carid.equals("")){
            jpa=jpa+" and carid= "+"'" +carid+"'";
        }
        Query q = entityManager.createQuery(jpa);
        Long count = (Long) q.getSingleResult();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }

    @RequestMapping("/Qtianjia")
    @ResponseBody
    public Map<String, Object> Qtianjia(int type,int carid, int ymoney, int pageSize, int pageNumber){
        Car car=new Car();
        car .setType(type);
        car.setCarid(carid);
        car.setYmoney(ymoney);
        carDao.save(car);
        return quanbujinayi(pageSize,pageNumber,"-1",null);
    }

    @RequestMapping("/Qss")
    @ResponseBody
    public Map<String ,Object> add2(Car car, int pageNumber, int pageSize){
        Car car1=new Car();
        car1.setCarid(car.getCarid());
        car1.setId(car.getId());
        car1.setName(car.getName());
        car1.setRemark(car.getRemark());
        car1.setDdate(car.getDdate());
        car1.setPhone(car.getPhone());
        car1.setCarnumber(car.getCarnumber());
        car1.setAddress(car.getAddress());
        car1.setYmoney(car.getYmoney());
        car1.setType(car.getType());
        car1.setQdate(car.getQdate());

        carDao.save(car1);

        return quanbujinayi(pageSize, pageNumber,"-1",null);
    }

    //删除
    @RequestMapping("/Qdel")
    @ResponseBody
    public Map<String ,Object> Qdel(long id,int pageSize,int pageNumber){
        carDao.deleteById(id);
        return quanbujinayi(pageSize, pageNumber,"-1",null);
    }



}
